package com.xiaochong.tir.apiservice.service;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.hbasepo.*;
import com.xiaochong.tir.common.data.vo.ApiMarketAnalysisTotalResult;
import com.xiaochong.tir.common.data.vo.app.VxGlobalIndexVo;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @Description: 大盘强弱
 * @Author: org
 * @CreateDate: 2018/9/13 16:54
 */
@Service
public class CmcMarketService {

    @Autowired
    private RedisServer redisServer;

    public ApiMarketAnalysisTotalResult getMarketAnalysisTotalResult() {
        ApiMarketAnalysisTotalResult vo = new ApiMarketAnalysisTotalResult();
        try {
            //去redis中取量能变化、赚钱效应、权重表现  WeightsPerformance
            String grailIndex = redisServer.get(RedisKeysUtils.INDEX_EVERY_DAY_RECORDING);
            LogTrace.info("大盘分析-量能变化、赚钱效应、权重表现redis数据","数据",grailIndex);
            //去redis涨跌统计
            String highOrLow = redisServer.get(RedisKeysUtils.TIR_CHANGE_STATISTICS);
            LogTrace.info("大盘分析-涨跌统计redis数据","数据",highOrLow);
            if(StringUtils.isBlank(grailIndex) && StringUtils.isBlank(highOrLow)){
                return null;
            }
            Map<String,Object> marketMap = JSON.parseObject(grailIndex);
            BigDecimal scoreMoneyEffect;
            BigDecimal scoreWeightsPerformance;
            BigDecimal scoreStatistics ;
            BigDecimal scoreQuantumEnergy ;
            BigDecimal total = new BigDecimal(0);
            if(marketMap != null){
                for (Map.Entry m :marketMap.entrySet()){
                    if("moneyEffect".equals(m.getKey())){ //MoneyEffect
                        MoneyEffect bi = JSON.parseObject(JSON.toJSONString(m.getValue()),MoneyEffect.class);
                        if(bi != null && bi.getScore() != null){
                            LogTrace.info("大盘强度分析","赚钱效应分数",String.valueOf(bi.getScore()));
                            scoreMoneyEffect = new BigDecimal(bi.getScore()).multiply(BigDecimal.valueOf(0.15));
                            total = total.add(scoreMoneyEffect);
                            vo.setMakeMoneyEffect(bi.getComment()!=null?bi.getComment():"");
                        }
                    }else if("weightsPerformance".equals(m.getKey())){
                        WeightsPerformance bi = JSON.parseObject(JSON.toJSONString(m.getValue()),WeightsPerformance.class);
                        if(bi != null && bi.getScore() != null){
                            scoreWeightsPerformance = new BigDecimal(bi.getScore()).multiply(BigDecimal.valueOf(0.3));
                            total = total.add(scoreWeightsPerformance);
                            LogTrace.info("大盘强度分析","权重表现分数",String.valueOf(bi.getScore()));
                            vo.setWeightPerformance(bi.getComment()!=null?bi.getComment():"");
                        }
                    }else if("quantumEnergy".equals(m.getKey())){
                        QuantumEnergy bi = JSON.parseObject(JSON.toJSONString(m.getValue()),QuantumEnergy.class);
                        if(bi != null && bi.getScore() != null){
                            scoreQuantumEnergy = new BigDecimal(bi.getScore()).multiply(BigDecimal.valueOf(0.25));
                            total = total.add(scoreQuantumEnergy);
                            LogTrace.info("大盘强度分析","量能变化分数",String.valueOf(bi.getScore()));
                            vo.setEnergyChange(bi.getComment()!=null?bi.getComment():"");
                        }
                    }
                }
            }
            if (StringUtils.isNotBlank(highOrLow)){
                CurrentcyHighOrLow hl = JSON.parseObject(highOrLow,CurrentcyHighOrLow.class);
                scoreStatistics = new BigDecimal(Double.valueOf(hl.getScore())).multiply(BigDecimal.valueOf(0.3));
                total = total.add(scoreStatistics);
                LogTrace.info("大盘强度分析","涨跌统计分数分数",String.valueOf(hl.getScore()));
                vo.setStatistics(hl.getEvaluate());
            }
            //计算4个比例分数，以及给出评语
            LogTrace.info("大盘强度","total分数",total.toString());
            vo.setMarketScore(String.valueOf(total.setScale(1,BigDecimal.ROUND_DOWN)));
            double num = Double.valueOf(String.valueOf(total));
            if(num < 4){
                vo.setResult("大盘处于走弱态势");
            }else if(num >= 4 && num < 6){
                vo.setResult("大盘处于盘整态势");
            }else{
                vo.setResult("大盘处于走强态势");
            }
            //获取近7日趋势
            vo.setLast7status(redisServer.get(RedisKeysUtils.BROADER_DATA_7_STATUS));
            //获取近30日趋势
            vo.setLast30status(redisServer.get(RedisKeysUtils.BROADER_DATA_30_STATUS));
            redisServer.set(RedisKeysUtils.INDEX_GRAIL_STRONG,JSON.toJSONString(vo));
            return vo;
        }catch (Exception e){
            LogTrace.error("获取大盘解析",e);
            e.printStackTrace();
            return null;
        }
    }

    public void indexGlobal() {
        VxGlobalIndexVo result = new VxGlobalIndexVo();
        //权重指数和涨跌
        String weightIndex = redisServer.get(RedisKeysUtils.TIR_WEIGHT_INDEX);
        if(StringUtils.isNotBlank(weightIndex)){
            WeightIndex wi = JSON.parseObject(weightIndex,WeightIndex.class);
            result.setWeightIndex(wi.getWeightIndex()!=null?wi.getWeightIndex().setScale(2,BigDecimal.ROUND_DOWN).toString():"");
            result.setWeightChange(wi.getWeightRiseFall()!=null?wi.getWeightRiseFall().multiply( new BigDecimal( 100 ) ).setScale(2,BigDecimal.ROUND_DOWN).toString():"0");
            if(null != wi.getCreateTime()){
                result.setDatetime(String.valueOf(wi.getCreateTime().getTime()));
            }
        }
        //大盘指数和涨跌
        String grailIndex = redisServer.get(RedisKeysUtils.TIR_BROADER_INDEX);
        if(StringUtils.isNotBlank(grailIndex)){//权重指数和涨跌
            BroaderIndex bi = JSON.parseObject(grailIndex,BroaderIndex.class);
            result.setGrailIndex(bi.getBroaderIndex()!=null?bi.getBroaderIndex().setScale(2,BigDecimal.ROUND_DOWN).toString():"");
            result.setGrailChange(bi.getBroaderRiseFall()!=null?bi.getBroaderRiseFall().multiply( new BigDecimal( 100 ) ).setScale(2,BigDecimal.ROUND_DOWN).toString():"0");
            if(null != bi.getCreateTime() && StringUtils.isBlank(result.getDatetime()) ){
                result.setDatetime(String.valueOf(bi.getCreateTime().getTime()));
            }
        }
        //权重市值和交易量
        String global = redisServer.get(RedisKeysUtils.CMC_GLOBAL_LAST_DATA);
        if(StringUtils.isNotBlank(global)){
            CmcCurrencyGlobalInfo cmcCurrencyGlobalInfo = JSON.parseObject(global,CmcCurrencyGlobalInfo.class);
            if(cmcCurrencyGlobalInfo != null){
                result.setTotalMarketCap(cmcCurrencyGlobalInfo.getTotalMarketCapUsd()!=null?cmcCurrencyGlobalInfo.getTotalMarketCapUsd():"0");
                result.setTotalVolume24h(cmcCurrencyGlobalInfo.getTotal24hVolumeUsd()!=null?cmcCurrencyGlobalInfo.getTotal24hVolumeUsd():"0");
            }
        }
        //涨跌数量
        String fallStr=redisServer.get(RedisKeysUtils.TIR_CHANGE_STATISTICS);
        if(StringUtils.isNotBlank(fallStr)){
            CurrentcyHighOrLow curentcyHigh=JSON.parseObject(fallStr, CurrentcyHighOrLow.class);
            if(curentcyHigh != null){
                result.setCurrencyUpCount(String.valueOf(curentcyHigh.getUpCount()));
                result.setCurrencyDownCount(String.valueOf(curentcyHigh.getDownCount()));
            }
        }

        //大盘解析
        result.setExplain(redisServer.get(RedisKeysUtils.BROADER_DATA_7_STATUS));
       // String grail = redisServer.get(RedisKeysUtils.INDEX_GRAIL_STRONG);
        /*if(StringUtils.isNotBlank(grail)){
            ApiMarketAnalysisTotalResult m = JSON.parseObject(grail,ApiMarketAnalysisTotalResult.class);
        }else{
            result.setExplain("-");
        }*/
        redisServer.set(RedisKeysUtils.GLOBAL_INDEX_VX_ROBOT,JSON.toJSONString(result));
    }
}
